﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApp.BaseDataSvc;
using YY.Common;

namespace WebApp
{
    public partial class EditClientFrm : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(EditClientFrm), this);
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<TB_Clients> GetClient(int id)
        {
            AjaxResult<TB_Clients> result = new AjaxResult<TB_Clients>();
            result.Data = null;
            result.Msg = null;
            try
            {
                result.Data = this.BaseDataProxy.GetClient(id);
            }
            catch (Exception ex)
            {
                result.Data = null;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<int> SaveClient(string id, string shortName, string fullName,string contactMan, string tel,string fax,string address,string memo,string bankAccount)
        {
            AjaxResult<int> result = new AjaxResult<int>();
            result.Data = 0;
            result.Msg = null;
            bool update = !string.IsNullOrEmpty(id);
            try
            {

                TB_Clients client = new TB_Clients();
                if (update)
                    client = this.BaseDataProxy.GetClient(Convert.ToInt32(id));

                WebCommon.EvalControlValueToData(client, "shortName", shortName);
                WebCommon.EvalControlValueToData(client, "fullName", fullName);
                WebCommon.EvalControlValueToData(client, "contactMan", contactMan);
                WebCommon.EvalControlValueToData(client, "tel", tel);
                WebCommon.EvalControlValueToData(client, "fax", fax);
                WebCommon.EvalControlValueToData(client, "address", address);
                WebCommon.EvalControlValueToData(client, "memo", memo);
                WebCommon.EvalControlValueToData(client, "bankAccount", bankAccount);

                if (update)
                {
                    this.BaseDataProxy.UpdateClient(client);
                    result.Data = client.id;
                }
                else
                {
                    client.deleteFlag = false;
                    result.Data = this.BaseDataProxy.AddClient(client);
                }
            }
            catch (Exception ex)
            {
                result.Data = 0;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> DeleteClient(int id)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Delete, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                result.Data = this.BaseDataProxy.DeleteClient(id);
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }
    }
}
